//
//  EOTools.m
//  EduOnLine
//
//  Created by Mac on 2019/11/14.
//  Copyright © 2019 eduonline. All rights reserved.
//

#import "RHTools.h"
#import "SAMKeychain.h"

@implementation RHTools

- (NSString *)randomStringWithLength:(int)len{
    NSString *letterd =@"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789#&@!?";
    NSMutableString *randomString = [NSMutableString stringWithCapacity: len];
    
    for (NSUInteger i = 0; i < len; i++) {
        u_int32_t r = arc4random() % [letterd length];
        unichar c = [letterd characterAtIndex:r];
        [randomString appendFormat:@"%C", c];
    }
    NSLog(@"random string:%@",randomString);
    return randomString;
}

+ (void)setAccount:(NSString *)account password:(NSString *)password{
    NSString *bundleID = kbundleIdentifier;//获得本应用的bundle ID
    NSArray *accountList = [SAMKeychain accountsForService:bundleID];
    NSLog(@"bundle id = %@", bundleID);
    for (NSDictionary *dic in accountList) {
        NSString *acct = [dic objectForKey:@"acct"];
        if (![acct isEqualToString:account]) {
            [SAMKeychain deletePasswordForService:bundleID account:acct];
        }
    }
    
    [SAMKeychain setPassword:password forService:bundleID account:account];
}

+ (NSDictionary *)getAccountInfo{
    NSString *bundleID = kbundleIdentifier;
    NSArray *accountList = [SAMKeychain accountsForService:bundleID];
    if (accountList.count>0) {
        NSString *account = [[accountList firstObject] objectForKey:@"acct"];
        NSString *password =  [SAMKeychain passwordForService:bundleID account:account];
        NSDictionary *dic = @{@"account":account,@"password":password?password:@""};
        return dic;
    }else{
        return nil;
    }
}

//获取当前屏幕显示的viewcontroller
+ (UIViewController *)getCurrentVC
{
    UIViewController *rootViewController = [UIApplication sharedApplication].keyWindow.rootViewController;
    
    UIViewController *currentVC = [[self class] getCurrentVCFrom:rootViewController];
    
    return currentVC;
}

+ (UIViewController *)getCurrentVCFrom:(UIViewController *)rootVC
{
    UIViewController *currentVC;
    
    if ([rootVC presentedViewController]) {
        // 视图是被presented出来的
        
        rootVC = [rootVC presentedViewController];
    }
    
    if ([rootVC isKindOfClass:[UITabBarController class]]) {
        // 根视图为UITabBarController
        
        currentVC = [[self class] getCurrentVCFrom:[(UITabBarController *)rootVC selectedViewController]];
        
    } else if ([rootVC isKindOfClass:[UINavigationController class]]){
        // 根视图为UINavigationController
        
        currentVC = [[self class] getCurrentVCFrom:[(UINavigationController *)rootVC visibleViewController]];
        
    } else {
        // 根视图为非导航类
        
        currentVC = rootVC;
    }
    
    return currentVC;
}

/**
 未知类型（仅限字典/数组/字符串）
 
 @param object 字典/数组/字符串
 @return 字符串
 */
+(NSString *) jsonStringWithObject:(id) object{
    NSString *value = nil;
    if (!object) {
        return value;
    }
    if ([object isKindOfClass:[NSString class]]) {
        value = [self jsonStringWithString:object];
    }else if([object isKindOfClass:[NSDictionary class]]){
        value = [self jsonStringWithDictionary:object];
    }else if([object isKindOfClass:[NSArray class]]){
        value = [self jsonStringWithArray:object];
    }
    return value;
}

/**
 字符串类型转JSON
 
 @param string 字符串类型
 @return 返回字符串
 */
+(NSString *) jsonStringWithString:(NSString *) string{
    return [NSString stringWithFormat:@"%@",
            [[string stringByReplacingOccurrencesOfString:@"\n" withString:@"\\n"] stringByReplacingOccurrencesOfString:@"\"" withString:@"\\\""]
            ];
}

/**
 数组类型转JSON
 
 @param array 数组类型
 @return 返回字符串
 */
+(NSString *) jsonStringWithArray:(NSArray *)array{
    NSMutableString *reString = [NSMutableString string];
    [reString appendString:@"["];
    NSMutableArray *values = [NSMutableArray array];
    for (id valueObj in array) {
        NSString *value = [self jsonStringWithObject:valueObj];
        if (value) {
            [values addObject:[NSString stringWithFormat:@"%@",value]];
        }
    }
    [reString appendFormat:@"%@",[values componentsJoinedByString:@","]];
    [reString appendString:@"]"];
    return reString;
}

/**
 字典类型转JSON
 
 @param dictionary 字典数据
 @return 返回字符串
 */
+(NSString *) jsonStringWithDictionary:(NSDictionary *)dictionary{
    NSError *parseError = nil;
    
    NSData *jsonData = [NSJSONSerialization dataWithJSONObject:dictionary options:NSJSONWritingPrettyPrinted error:&parseError];
    
    return [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding];
}

+ (id)getJsonDataJsonname:(NSString *)jsonname
{
    NSString *path = [[NSBundle mainBundle] pathForResource:jsonname ofType:@"json"];
    NSData *jsonData = [[NSData alloc] initWithContentsOfFile:path];
    NSError *error;
    id jsonObj = [NSJSONSerialization JSONObjectWithData:jsonData options:NSJSONReadingMutableContainers error:&error];
    if (!jsonData || error) {
        //DLog(@"JSON解码失败");
        return nil;
    } else {
        return jsonObj;
    }
}



#pragma mark -------- 角色 ---------
+(NSString*)getRoleContentWithType:(OrganizationType)type brandId:(NSInteger)brandId{
    NSInteger partnerType = type;
    switch (partnerType) {
        case 0:
            return @"品牌商".rh_localized;
        case 1:
            return @"运维".rh_localized;
            break;
        
        case 2:
            return @"安装商".rh_localized;
            break;
        case 3:
        {
            if (brandId == brand_id.integerValue) {
                return @"游客".rh_localized;
            }else{
                return @"终端用户".rh_localized;
            }
            break;
        }
        default: return @"未知".rh_localized;
            break;
    }
}


#pragma mark ---------- 地区 -----
+(NSString*)getRegionNameWithCode:(NSString*)code
{
    NSString *region = @"";
    if([code isEqualToString:@"520000000000"]){
        region = @"中国".rh_localized;
    }else if([code isEqualToString:@"530000000000"]){
        region = @"韩国".rh_localized;
    }else if([code isEqualToString:@"550000000000"]){
        region = @"日本".rh_localized;
    }else if([code isEqualToString:@"560000000000"]){
        region = @"美国".rh_localized;
    }
    
    return region;
}

+(NSString*)getAreaNameWithAreaCode:(NSString*)code
{
    NSString *region = code;
    if([code isEqualToString:@"CN"]){
        region = @"大陆".rh_localized;
    }else if([code isEqualToString:@"US"]){
        region = @"北美洲".rh_localized;
    }else if([code isEqualToString:@"HK"]){
        region = @"东亚".rh_localized;
    }
    
    return region;
}

+(NSString*)getCurrentCurrencyType
{
    //当前语言
    NSString *selectLanguage = [NSString getCurrentLanguage];
    //除了中文,其他一概设成英文
    BOOL isChineseLanguage = [selectLanguage containsString:@"zh"]; //中文
    
    //货币种类 0:人民币(CNY)  1:美元(USD) 2:欧元(EUR) 3:日元(JPY) 4:英镑(GBP)  5:澳元(AUD)
    NSString *currencyType = isChineseLanguage?@"人民币":@"CNY";
   
    NSInteger type = [PrivateMange share].currentUser.currency;
    switch (type) {
        case 1:
            currencyType = isChineseLanguage?@"美元":@"USD";
            break;
        case 2:
            currencyType = isChineseLanguage?@"欧元":@"EUR)";
            break;
        case 3:
            currencyType = isChineseLanguage?@"日元":@"JPY";
            break;
        case 4:
            currencyType = isChineseLanguage?@"英镑":@"GBP";
            break;
        case 5:
            currencyType = isChineseLanguage?@"澳元":@"AUD";
            break;
        default:
            break;
    }
    return currencyType;
}


+(NSArray*)allConfigedInverterType
{
    return @[
            InvType_PCS_BCS_B_HM_USX2,
            InvType_InvMegarevoLv12KW,
            InvType_Sol_Ark_30K_3P_208V,
            InvType_Sol_Ark_60K_3P_480V,
            InvType_solis_ENS33000ID,
            InvType_InvHISOLAR15KW,
            InvType_InvSolisLv16KW
            
            ];
}

@end
